package com.yanqu.road.dao.impl.activity.lottery;

import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.db.DbParameter;
import com.yanqu.road.dao.impl.TempDataObjectDao;
import com.yanqu.road.entity.activity.lottery.ActivityLotteryBox;
import com.yanqu.road.utils.string.StringUtils;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.HashMap;
import java.util.Map;

public class ActivityLotteryBoxDaoImpl extends TempDataObjectDao<ActivityLotteryBox> {


    public static String getActivityLotteryBoxSql = "select * from `t_c_activity_lottery_box` where `activityId`=?";
    public ActivityLotteryBox getActivityLotteryBox(int activityId) {
        ResultSet rs = null;
        CallableStatement call = null;
        try {
            getDb().setConnName(DbNameStrategy.getCross2Db());
            call = getDb().getConnection().prepareCall(getActivityLotteryBoxSql);
            int i = 1;
            call.setInt(i++, activityId);
            rs = call.executeQuery();
            if (rs.next()) {
                return parse(rs);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);} finally {
            getDb().closeConnection(call, rs);
        }
        return null;
    }

    public static String updateActivityLotteryBoxSql = "update `t_c_activity_lottery_box` set count=?,lastUpdateTime=?,userList=? where activityId=?";
    @Override
    protected boolean updateData(ActivityLotteryBox activityLotteryBox) {
        activityLotteryBox.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getCross2Db());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.INTEGER, activityLotteryBox.getCount()));
        para.put(i++, new DbParameter(Types.BIGINT, activityLotteryBox.getLastUpdateTime()));
        para.put(i++, new DbParameter(Types.VARCHAR, StringUtils.listToString(activityLotteryBox.getUserList(), ",")));
        para.put(i++, new DbParameter(Types.INTEGER, activityLotteryBox.getActivityId()));
        if (getDb().executeNoneQuery(updateActivityLotteryBoxSql, para) > 0) {
            return true;
        }
        return false;
    }

    public static String insertActivityLotteryBoxSql = "insert into `t_c_activity_lottery_box`(activityId,count,lastUpdateTime,userList) values (?,?,?,?)";
    @Override
    protected boolean addData(ActivityLotteryBox activityLotteryBox) {
        activityLotteryBox.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getCross2Db());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.INTEGER, activityLotteryBox.getActivityId()));
        para.put(i++, new DbParameter(Types.INTEGER, activityLotteryBox.getCount()));
        para.put(i++, new DbParameter(Types.BIGINT, activityLotteryBox.getLastUpdateTime()));
        para.put(i++, new DbParameter(Types.VARCHAR, StringUtils.listToString(activityLotteryBox.getUserList(), ",")));
        if (getDb().executeNoneQuery(insertActivityLotteryBoxSql, para) > 0) {
            return true;
        }
        return false;

    }

    @Override
    protected ActivityLotteryBox parseData(ResultSet rs) throws SQLException {
        ActivityLotteryBox activityLotteryBox = new ActivityLotteryBox();
        activityLotteryBox.setActivityId(rs.getInt("activityId"));
        activityLotteryBox.setCount(rs.getInt("count"));
        activityLotteryBox.setLastUpdateTime(rs.getLong("lastUpdateTime"));
        activityLotteryBox.setUserList(StringUtils.stringToLongList(rs.getString("userList"), ","));

        return activityLotteryBox;
    }
}
